﻿/*
thumbnail class 1.1
Mike Mariano
http://marianomike.com
*/

package com{
	import caurina.transitions.*;
	import fl.containers.UILoader;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.display.Sprite;
    import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldType;
	import flash.text.TextFieldAutoSize;
	import flash.text.AntiAliasType;
	import flash.text.Font;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.geom.*;

	
	public class Thumbnail extends Sprite {
		public var title:String;
		public var num:uint;
		public var _clicked:Boolean;
		
		private var thumbImage:String;
		private var thumb_loader:UILoader;
		private var title_txt:TextField;
		private var loadProgress_txt:TextField;
		private var square:Sprite;
		private var thumb_bg:Sprite;
		private var mask_mc:Sprite;
		private var roll:Sprite;
		private var outline_mc:Sprite;
		private var thumbWidth:uint;
		private var thumbHeight:uint;
		private var format:TextFormat;
		private var font:Font;
		private var outline_color:uint;
		private var txt_color:uint;
		private var textShow:Boolean;
		private var outline:uint;
		private var scaled:Boolean;
		public  var id;
		
		
		public var thumb_active = false;
		
		private var filtersArray:Array;
		
		public static var nb_img:Number = 0;
		
		var active_over = false;
		
		static var thumb_locked;
		
		static var current_thumb;
		

		function Thumbnail(thumbText:String, 
						   id:uint, 
						   image:String, 
						   thumb_height:Number, 
						   thumb_width:Number, 
						   bgColor:uint, 
						   outlineColor:uint, 
						   rolloverColor:uint,
						   outlineSize:uint, 
						   txtColor:uint, 
						   showText:Boolean, 
						   showDrop:Boolean,
						   scale:Boolean):void {
			
			
			this.title = thumbText;
			this._clicked = false;
			this.thumbImage = image;
			this.num = id;
			this.outline_color = outlineColor;
			this.txt_color = txtColor;
			this.thumbWidth = thumb_width;
			this.thumbHeight = thumb_height;
			this.textShow = showText;
			this.outline = outlineSize*0;
			this.scaled = scale;

			//enable button
			this.buttonMode = true;
			this.mouseChildren = false;
			
			//create drop shadow
			var dropShadow:DropShadowFilter = new DropShadowFilter();
			dropShadow.color = 0x000000;
			dropShadow.blurX = 5;
			dropShadow.blurY = 5;
			dropShadow.alpha = .75;
			dropShadow.distance = 2;
			filtersArray = new Array(dropShadow);
			
			//create mask
			if(this.scaled == false){
				mask_mc = new Sprite();
				mask_mc.graphics.beginFill(txtColor);
				mask_mc.graphics.drawRect(0,0,thumbWidth,thumbHeight);
				mask_mc.graphics.endFill();
				mask_mc.x = this.outline/2;
				mask_mc.y = this.outline/2;
				addChild(mask_mc);
			}
			
			//create outline
			outline_mc = new Sprite();
			outline_mc.graphics.beginFill(outlineColor);
			outline_mc.graphics.drawRect(0,0,thumbWidth+this.outline,thumbHeight+this.outline);
			outline_mc.graphics.endFill();
			
			//add drop shadow
			if(showDrop == true){
				outline_mc.filters = filtersArray;
			}
			
			//create rollover
			roll = new Sprite();
			roll.graphics.beginFill(rolloverColor);
			roll.graphics.drawRect(0,0,thumbWidth+this.outline,thumbHeight+this.outline);
			roll.graphics.endFill();
			roll.alpha = 0;
			//addChild(roll);
			
			//create background
			thumb_bg = new Sprite();
			//thumb_bg.graphics.beginFill(rolloverColor);
			thumb_bg.graphics.drawRect(0,0,thumbWidth+this.outline,thumbHeight+this.outline)
			thumb_bg.graphics.endFill();
			thumb_bg.alpha = 0;
			thumb_bg.x = this.outline/2;
			thumb_bg.y = this.outline/2;
			//thumb_bg.filters = filtersArray;
			addChild(thumb_bg);
			
			loadThumbImage();
			
			addChild(thumb_loader);
			
			//text format for title
			//var dirtyHouse = new DirtyHouse
			format = new TextFormat();
            format.font = 'DirtyHouse';
            format.color = 0xFFFFFF;
            format.size = 17;
			format.align = 'center';
			
			
			if( showText ) {
				drawText();
			}
			
			
			var title_zone = new MovieClip;
			title_zone.name = 'title_zone';
			
			var anim_cadrage = new Sprite;
			anim_cadrage.graphics.beginFill(0x000000);
			anim_cadrage.graphics.drawRect(0,0,thumbWidth+this.outline,thumbHeight)
			anim_cadrage.graphics.endFill();
			anim_cadrage.alpha = 0.5
			title_zone.addChild(anim_cadrage);
			title_zone.setChildIndex(anim_cadrage, 0)
			
			title_txt = drawText();
			
			title_txt.x = thumbWidth/2 - title_txt.width/2;
			title_txt.y = thumbHeight/2 - title_txt.height/2;
			
			title_zone.addChild(title_txt);
			title_zone.setChildIndex(title_txt, 1)
			title_zone.y = thumbHeight - anim_cadrage.height;
			title_zone.mask = mask_mc;
			title_zone.alpha = 0;
			

			this.x = (thumbWidth)*id+1;
			
			addChild(title_zone);
			
			this.name = 'thumb'+id;
			nb_img++;
			
			
			
		}
		
		public static function scaleAroundPoint(objToScale:DisplayObject, regX:int, regY:int, scaleX:Number, scaleY:Number):void{
			if (!objToScale){
				return;
			}
			var transformedVector:Point = new Point( (regX-objToScale.x)*scaleX, (regY-objToScale.y)*scaleY );
			
			objToScale.x = regX-( transformedVector.x);
			objToScale.y = regY-( transformedVector.y);
			objToScale.scaleX =  objToScale.scaleX*(scaleX);
			objToScale.scaleY =  objToScale.scaleY*(scaleY);
		}

		
		
		//load image
		private function loadThumbImage():void {

			thumb_loader = new UILoader();
			thumb_loader.source = thumbImage;
			thumb_loader.height = thumbHeight;
			thumb_loader.width = thumbWidth;
			
			Main.debug_txt.text += thumbImage+'<br/>';
			trace(1);
			
			thumb_loader.addEventListener(Event.OPEN,showPreloader);
			thumb_loader.addEventListener(ProgressEvent.PROGRESS,showProgress);
			thumb_loader.addEventListener(Event.COMPLETE,showLoadResult);
			thumb_loader.mouseEnabled = false;
			thumb_loader.x = this.outline/2;
			thumb_loader.y = this.outline/2;
			thumb_loader.maintainAspectRatio = true;
			thumb_loader.alpha = 1;
			thumb_loader.filters = filtersArray;
			
			if(this.scaled == false){
				thumb_loader.scaleContent = false;
				thumb_loader.mask = mask_mc;
			}

			
		}
		
		//create text
		private function drawText(){
			title_txt = new TextField();
			title_txt.width = thumbWidth;
			title_txt.height = thumbHeight;
			
			title_txt.htmlText = this.title;
			title_txt.selectable = false;
			title_txt.autoSize = TextFieldAutoSize.CENTER;
			title_txt.setTextFormat(format);
			title_txt.wordWrap = true;
			
			title_txt.embedFonts = true;
			
			title_txt.multiline = true;
			title_txt.mask = mask_mc;
			
			//title_txt.visible = false;
			title_txt.name = 'title';
			
			//title_txt.background = true;
			//title_txt.backgroundColor = 0xFFFFFF;
			title_txt.y = 0;
			return title_txt;

		}
		
		
		
		//preloader events
		private function showPreloader(evt:Event):void {
			//progress text
			loadProgress_txt = new TextField();
			loadProgress_txt.x = thumb_bg.width/2 - loadProgress_txt.width/2;
			loadProgress_txt.y = thumb_bg.height/2 - 5;
			loadProgress_txt.embedFonts = true;
			loadProgress_txt.selectable = false;
			loadProgress_txt.autoSize = TextFieldAutoSize.CENTER;
			loadProgress_txt.defaultTextFormat = format;
		    addChild(loadProgress_txt);
		}

		private function showProgress(evt:ProgressEvent):void {
			var percentLoaded:Number = evt.bytesLoaded/evt.bytesTotal;
			loadProgress_txt.text = Math.round(percentLoaded * 100) + "%";
		}
		
		//once image has loaded, remove progress textfield and show
		private function showLoadResult(evt:Event):void {
		    removeChild(loadProgress_txt);
			Tweener.addTween(thumb_loader, {alpha:1, time:1, transition:"easeOut"});
			this.enableRoll();
		}
		
		private function enableRoll():void{
			this.addEventListener(MouseEvent.MOUSE_OVER,onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT,onOut);
			this.addEventListener(MouseEvent.CLICK,onClick);
		}
		
		// Define public getter
		public function get clicked():Boolean {
			return _clicked;
		}
		
	

		
		
		
		// Define public setter 
		public function set clicked(value:Boolean):void {

			_clicked = value;
			
			trace( _clicked );
			
			/*
			if( thumb_locked != this ) {
				Tweener.addTween(thumb_locked, {alpha:1, time:1, transition:"easeOut"});
				thumb_locked = this;
			
			} else {
				
				
			}
			*/
			
			
			
			
			/*
			if(_clicked == true){
				//ADTECH.click('click_image_'+this.id);
				roll.alpha = 1;
			} else if(_clicked == false) {
				trace(thumb_locked.name);
				
				
				thumb_locked = this;
				Tweener.addTween(roll, {alpha:0, time:1, transition:"easeOut"});
				
				
			}*/
			
			
			
			
			
			
		}
		
		
		//mouse events
		function onOver(thumb):void {
			current_thumb = thumb.target;

			if( current_thumb._clicked == false ) {
				darken(current_thumb);
			}
		}
		
		
		//mouse events
		function onOut(thumb):void {
			current_thumb = thumb.target;
			
			if( current_thumb._clicked == false ) {
				lighten(current_thumb);
			}
		}
		
		
		function onClick(thumb:MouseEvent) {
			if( this._clicked == false ) {
				current_thumb = thumb.target;
				this._clicked = true;
				
				if( thumb_locked != null ) {
					lighten(thumb_locked);
					thumb_locked._clicked = false;
				}
				thumb_locked = this;
				
				Main.onClick(current_thumb);
			}
		}
		
		
		function darken(thumb) {
			var title_zone = MovieClip(thumb.getChildByName('title_zone'));
			Tweener.addTween(title_zone, {alpha:1, time:1, transition:"easeOut"});
		}	
		
		function lighten(thumb) {
			var title_zone = MovieClip(thumb.getChildByName('title_zone'));
			Tweener.addTween(title_zone, {alpha:0, time:1, transition:"easeOut"});
		}
	}
}

